<template>
  <div style="padding: 24px">
    <el-button type="primary" v-if="tableData.length < 5" @click="handleAdd"
      >新增</el-button
    >
    <h2>轮播图列表</h2>
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column type="index" width="50"> </el-table-column>
      <el-table-column label="图片" width="180">
        <template slot-scope="scope">
          <img width="100px" :src="scope.row.carouselPath" alt="" />
        </template>
      </el-table-column>
      <el-table-column prop="carouselUrl" label="链接"> </el-table-column>
      <el-table-column fixed="right" label="操作" width="100">
        <template slot-scope="scope">
          <el-button @click="handleDelete(scope.row)" type="text" size="small"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="添加图片" :visible.sync="dialogFormVisible" :close-on-click-modal="false">
      <el-form :model="form">
        <el-form-item label="选择图片" :label-width="formLabelWidth">
          <el-upload
            class="upload-demo"
            action="http://47.242.59.183:8084/bus/outer/importFile"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :on-success="handleSuccess"
            :file-list="form.carouselPath"
            list-type="picture"
          >
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">每次只能上传1张图片</div>
          </el-upload>
        </el-form-item>
        <el-form-item label="跳转链接" :label-width="formLabelWidth">
          <el-input v-model="form.carouselUrl" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import request from "@/utils/request";
export default {
  data() {
    return {
      tableData: [],
      dialogFormVisible: false,
      form: {
        carouselUrl: "",
        carouselPath: [],
      },
      formLabelWidth: "120px",
    };
  },
  created() {
    this.getCarouselList();
  },
  methods: {
    getCarouselList() {
      request({
        url: "/bus/carousel/list",
        method: "get",
      }).then((res) => {
        this.tableData = res.rows;
      });
    },
    handleAdd() {
      this.dialogFormVisible = true;
    },
    handleSubmit() {
      request({
        url: "/bus/carousel",
        method: "post",
        data: this.form,
      }).then((res) => {
        this.getCarouselList();
        alert("添加成功");
      });
      this.dialogFormVisible = false;
    },
    handleSuccess(res, file, fileList) {
      this.form.carouselPath = res.msg;
    },
    selectImg(e) {
      console.log(e);
    },
    handleDelete(e) {
      request({
        url: "/bus/carousel/" + e.carouselId,
        method: "delete",
      }).then((res) => {
        this.getCarouselList();
        alert("删除成功");
      });
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(file) {
      console.log(file);
    },
  },
};
</script>